<?php

/**
 * editad.php
 * @package adServer
 * @filesource
 */

/*	Close the backdoor	*/
$curPage = strtoupper($_SERVER['PHP_SELF']);
if (isset($curPage) && strpos($curPage, 'INDEX.PHP') === false)
    header('Location:./index.php');

require_once (CLASS_PATH . 'template.class.php');

/* Load the side menu */
$sideMenu = file_get_contents(TEMPLATE_PATH . 'sideMenu.tpl');

/*	load the page header	*/
$header = new template(TEMPLATE_PATH . 'header.tpl');
$header->replace('&&SIDEMENU&&', $sideMenu);
$header->replace('&&STYLE&&', $session->loggedIn ? "style='display:block;'" : "");
$header->replace('&&USER&&', $session->username);
/*	Load the page content	*/
$pageContent = new template(TEMPLATE_PATH . $nextAction . '.tpl');


if ($_POST['submit'] == 'Update') {
    $error = '';
    $success = '';
    $adUnitId = $_POST['adUnitId'];
    $adType = $_POST['adTypeId'];
    $adDuration = $_POST['adDuration'];
    $adStartOffset = $_POST['adStartOffsetPercent'];
    $adCampaign = $_POST['adCampaign'];
    $adLanguage = $_POST['adLanguageISO639'];
    $adHtml = $_POST['adHtml'];
    if ((int)$adDuration < 1)
        $error .= 'The Ad duration needs to be minimum 1 second<br/>';
    if ((int)$adDuration > 120)
        $error .= "The Ad duration needs to be maximum 120 seconds<br/>";
    if (strlen($adHtml) < 15 || substr_count($adHtml, '<') < 2 || substr_count($adHtml,
        '>') < 2)
        $error .= "The HTML doesnt seem to be valid<br/>";
    if ($error == '') {
        $session->updateAdUnit($adUnitId, $adType, $adCampaign, $adDuration, $adStartOffset,
            $adHtml, $adLanguage);
        $success = 'Advertisement unit updated successfully';
    } else {

    }
}

if (isset($_REQUEST['adUnitId'])) {
    $adUnitId = mysql_real_escape_string($_REQUEST['adUnitId']);
    if (isset($error) && $error != '') {
        $adDetails = $_POST;
    } else {
        $results = $session->getAdDetails($adUnitId);
        $adDetails = $results[1];
    }


    /*	Load the options from Db */

    $adTypes = $session->dbHandle->get_results(SQL_GET_TYPES);
    $adLanguages = $session->dbHandle->get_results(SQL_GET_LANGUAGES);
    $adCampaigns = $session->dbHandle->get_results(SQL_GET_CAMPAIGNS);

    $adTypesHtml = '';
    $selected = '';
    foreach ($adTypes as $result) {
        if (isset($adDetails['adTypeId']) && $adDetails['adTypeId'] == $result['typeId'])
            $selected = 'selected="selected"';
        else
            $selected = '';
        $adTypesHtml .= "<option $selected value ='" . $result['typeId'] . "'>" .
            $result['typeName'] . '</option>';
    }

    $adLanguagesHtml = '';

    foreach ($adLanguages as $result) {
        if (isset($adDetails['adLanguageISO639']) && $adDetails['adLanguageISO639'] ==
            $result['code'])
            $selected = 'selected="selected"';
        else
            $selected = '';
        $adLanguagesHtml .= "<option $selected value ='" . $result['code'] .
            "'>" . $result['language'] . '</option>';
    }

    $adStartOffsetHtml = '';
    for ($a = 0; $a <= 100; $a = $a + 10) {
        if (isset($adDetails['adStartOffsetPercent']) && (int)$adDetails['adStartOffsetPercent'] ==
            $a)
            $selected = 'selected="selected"';
        else
            $selected = '';
        $adStartOffsetHtml .= "<option $selected value ='" . $a . "'>" . $a .
            ' %</option>';
    }

    $adCampaignsHtml = '';

    foreach ($adCampaigns as $result) {
        if (isset($adDetails['adCampaign']) && $adDetails['adCampaign'] == $result['campaign'])
            $selected = 'selected="selected"';
        else
            $selected = '';
        $adCampaignsHtml .= "<option $selected value ='" . $result['campaign'] .
            "'>" . $result['campaign'] . '</option>';
    }

    /* Populate the template */
    $pageContent->replace('&&ADTYPE&&', $adTypesHtml);
    $pageContent->replace('&&ADLANGUAGES&&', $adLanguagesHtml);
    $pageContent->replace('&&ADSTART&&', $adStartOffsetHtml);
    $pageContent->replace('&&ADCAMPAIGN&&', $adCampaignsHtml);
    $pageContent->replace('&&ADTYPEVAL&&', $_GET['adType']);
    $pageContent->replace('&&ADDURATIONVAL&&', $adDetails['adDuration']);
    $pageContent->replace('&&ADHTMLVAL&&', stripslashes($adDetails['adHtml']));
    $pageContent->replace('&&ADUNITID&&', $adDetails['adUnitId']);
    $pageContent->replace('&&ERRMSG&&', $error);
    $pageContent->replace('&&SUCCESSMSG&&', $success);


    /*	Echo the page	*/
    echo $header->templateHtml;
    echo $pageContent->templateHtml;

} else {
    header('Location:./index.php');
}

?>